package com.gx.smart.building.ui.assets

import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.DividerItemDecoration
import com.blankj.utilcode.util.KeyboardUtils
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMActivityV3
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivityAssetsBinding
import com.gx.smart.building.ui.assets.adapter.AssetsDeviceAdapter
import com.gx.smart.building.ui.assets.adapter.AssetsTypeAdapter
import com.gx.smart.building.ui.assets.event.AssetsMessageEvent
import com.gx.smart.building.ui.assets.viewmodel.AssetsViewModel
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetDto
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetTypeDto
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class AssetsActivity : BaseMVVMActivityV3<AssetsViewModel>(),
    View.OnClickListener,
    TextView.OnEditorActionListener,
    BaseAdapter.OnItemClickListener<AssetTypeDto> {
    private lateinit var viewBinding: ActivityAssetsBinding
    private lateinit var typeAdapter: AssetsTypeAdapter
    private lateinit var deviceAdapter: AssetsDeviceAdapter
    private lateinit var assetType: List<AssetTypeDto>
    private lateinit var assetNewType: List<AssetTypeDto>
    private lateinit var assetTypeId: String
    override fun onBindViewModel(): Class<AssetsViewModel> = AssetsViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_assets

    override fun getToolbarTitle(): String = "设备台账"

    override fun initContentView(root: ViewGroup?) {
        val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val drawable = resources.getDrawable(R.drawable.shape_list_item_divider_line)
        divider.setDrawable(drawable)
        viewBinding = ActivityAssetsBinding.inflate(layoutInflater, root, true)
        typeAdapter = AssetsTypeAdapter(this)
        deviceAdapter = AssetsDeviceAdapter(this)
        deviceAdapter.setItemClickListener(object : BaseAdapter.OnItemClickListener<AssetDto> {
            override fun onItemClick(e: AssetDto, position: Int) {
                val intent = Intent(this@AssetsActivity, AssetsDetailActivity::class.java)
                intent.putExtra("id", e.id)
                if (e.deviceId.isNotEmpty()) {
                    intent.putExtra("deviceId", e.deviceId)
                }
                startActivity(intent)
            }
        })
        viewBinding.typeRecyclerView.adapter = typeAdapter
        viewBinding.typeRecyclerView.addItemDecoration(divider)
        viewBinding.deviceRecyclerView.adapter = deviceAdapter
        viewBinding.deviceRecyclerView.addItemDecoration(divider)
        viewBinding.search.setOnEditorActionListener(this)
        viewBinding.clear.setOnClickListener(this)
        typeAdapter.setItemClickListener(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterEventBus()
    }

    override fun initData() {
        registerEventBus()
        if (!this::assetNewType.isInitialized) {
            viewModel.queryAssetTypeTree().observe(this) {
                assetType = it
                typeAdapter.addAll(it)
            }
        } else {
            this.assetType = assetNewType
            typeAdapter.refresh(assetNewType)
            if (assetType.isEmpty()) {
                viewBinding.typeRecyclerView.visibility = View.GONE
            } else {
                viewBinding.typeRecyclerView.visibility = View.VISIBLE
            }
            viewModel.queryAssetList(assetTypeId).observe(this) {
                deviceAdapter.refresh(it)
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.clear -> viewBinding.search.editableText.clear()
        }
    }

    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            search()
            KeyboardUtils.hideSoftInput(v!!)
            return true
        }
        return false
    }

    private fun search() {
        val name = viewBinding.search.text.toString().trim()
        viewModel.queryAssetListByKeyword(name).observe(this) {
            deviceAdapter.refresh(it)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun handleEvent(event: AssetsMessageEvent) {
        assetNewType = event.item
        assetTypeId = event.assetTypeId
    }

    override fun onItemClick(e: AssetTypeDto, position: Int) {
        assetType.let { it ->
            startActivity(Intent(this, AssetsActivity::class.java))
            EventBus.getDefault()
                .postSticky(AssetsMessageEvent(e.id, it[position].assetTypeDtoList))
        }
    }


}